package com.leetcode.partition4;

/**
 * @author `RKC`
 * @date 2021/8/8 11:03
 */
public class LC343整数拆分 {

    public static int integerBreak(int n) {
        return dynamicProgramming(n);
    }

    public static void main(String[] args) {
        System.out.println(integerBreak(10));
    }

    private static int dynamicProgramming(int n) {
        //dp[i]代表拆分整数i能得到的最大乘积
        int[] dp = new int[n + 1];
        dp[2] = 1;
        for (int i = 3; i <= n; i++) {
            for (int j = 1; j < i - 1; j++) {
                //dp[i]可以从j * (i - j)或者j * dp[i - j]得到
                dp[i] = Math.max(dp[i], Math.max(dp[i - j] * j, j * (i - j)));
            }
        }
        return dp[n];
    }
}
